<!--项目-勘探点-分配人员列表-->
<template>
  <div class="app-container horizontal">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form
        ref="queryForm"
        :inline="true"
        label-width="80px"
        :model="filters"
      >
        <el-form-item label="人员姓名" prop="userName">
          <el-input
            v-model="filters.userName"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input
            v-model="filters.idCard"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <el-form-item label="确认状态" prop="consentFlag">
          <el-select
            v-model="filters.consentFlag"
            size="small"
            filterable
            placeholder="请选择用户确认状态"
            clearable
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="true"
          />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery()">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery()">重置</el-button>
      <el-button icon="el-icon-plus" size="middle" @click="personalAdd()">新增</el-button>
      <el-button icon="el-icon-user" size="middle" :disabled="single" @click="handleDelete()">取消分配人员</el-button>
      <span class="personal-type">
        <span>{{ explorationCode }} — {{ personTypeName }}</span>
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="
        highLevelSet === true
          ? needTagsView === true
            ? 'el-body-page-roll-tag-height'
            : 'el-body-page-roll-height'
          : needTagsView === true
            ? 'el-body-page-roll-tag'
            : 'el-body-page-roll'
      "
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="userName"
        label="人员姓名"
        min-width="60"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="idCard"
        label="身份证号"
        min-width="120"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="enterpriseName"
        label="所属企业"
        min-width="200"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="状态"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="{row}">
          <span v-if="row.consentFlag==='0'">同意</span>
          <span v-if="row.consentFlag==='1'">拒绝</span>
          <span v-if="row.consentFlag==='2'">等待中</span>
          <span v-if="row.consentFlag==='3'">转让</span>
          <span v-if="row.consentFlag==='4'">已取消</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 表格展示end-->

    <!-- 人员选择组件 -->
    <userTemplate
      ref="userTemplate"
      :get-person-list="selectProUserNotPermissionList"
      @fatherMethod="fatherMethod"
    />
  </div>
</template>

<script>
// 页面接口
import {
  listProUserExploration,
  listProUserExplorationAdd,
  selectProUserNotPermissionList,
  deleteProUserExploration
} from '@/api/projectManage/ProjectAllocation/proExplorationPersonnel'
// 页面组件
// 人员选择组件
import userTemplate from '@/components/basicinfo/userTemplate/index'
import { mapState } from 'vuex'
export default {
  components: {
    userTemplate
  },
  data() {
    return {
      statusList: [
        { label: '同意', value: '0' },
        { label: '拒绝', value: '1' },
        { label: '等待中', value: '2' },
        { label: '转让', value: '3' },
        { label: '已取消', value: '4' }
      ],
      selectProUserNotPermissionList, // 分配人员弹层接口
      explorationCode: '', // 勘探点编号
      personTypeName: '', // 人员类型名称
      /* 搜索条件 start*/
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        userName: '', // 人员姓名
        idCard: '', // 身份证号
        explorationId: '', // 勘探点id
        userType: '', // 分配人员类型
        consentFlag: '' // 确认状态
      },
      dateRange: [], // 日期范围
      /* 搜索条件 end*/

      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [], // 列表数据
      /* 列表 end*/
      userId: '', // 选择用户
      addUserVisible: false,
      status: '' // 项目状态
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView,
      personTypeList: state => state.basicInfo.personType // 人员类型
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    this.filters.userType = this.$route.query.personType.toString()
    this.filters.explorationId = this.$store.state.basicInfo.explorationId
    this.filters.explorationType = this.$route.query.explorationType
    this.filters.proId = this.$store.state.basicInfo.proId
    this.explorationCode = this.$route.query.explorationCode
    this.personTypeName = this.$route.query.personTypeName
    this.getList()
  },
  methods: {
    /* 列表 start*/
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 获取列表
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listProUserExploration(this.changeDateRange(this.filters, this.dateRange))
        this.loading = false
        this.tableData = rows
        this.total = total
        // this.addTaleData()
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 删除
    handleDelete() {
      // 获取选中行的id
      const child = this.multipleSelection
      if (child.length === 0) {
        this.msgInfo('请选择需要取消分配的人员!')
      } else {
        this.$confirm('确认要取消分配此用户吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            this.loading = true
            let ids = ''
            for (let i = 0; i < child.length; i++) {
              ids += child[i].proUserExplorationId + ','
            }
            // 去掉最后一个逗号
            if (ids.length > 0) {
              ids = ids.substr(0, ids.length - 1)
            }
            // const id = { userType: child[0].sysUserType, userId: ids, proId: this.filters.proId, explorationId: this.filters.explorationId }
            await deleteProUserExploration({ ids })
            this.loading = false
            this.msgSuccess('取消分配成功！')
            // 重新加载表格数据
            this.getList()
          })
          .catch(() => {})
      }
    },
    /* 列表 end*/

    /* 新增人员 start*/
    async fatherMethod(id, row, name) {
      const { explorationType, explorationId, proId } = this.filters
      if (row.length > 0) {
        const data = row.map(item => {
          return {
            userId: item.userId, proId, explorationId,
            userType: item.sysUserType, explorationType
          }
        })
        await listProUserExplorationAdd(data)
        this.msgSuccess('保存成功')
        this.resetQuery()
      }
    },
    personalAdd() {
      this.childrenTitle = `请选择` + this.personTypeName
      if (this.multipleSelection[0]) {
        this.$refs.userTemplate.handleAdd(
          this.childrenTitle,
          '0',
          this.filters.userType,
          this.filters.proId,
          this.filters.explorationId,
          this.multipleSelection[0].personId
        )
      } else {
        this.$refs.userTemplate.handleAdd(
          this.childrenTitle,
          '0',
          this.filters.userType,
          this.filters.proId,
          this.filters.explorationId
        )
      }
    },
    /* 新增人员 end*/

    // 返回
    handleReturn() {
      this.$store.state.tagsView.visitedViews.splice(
        this.$store.state.tagsView.visitedViews.findIndex(
          item => item.path === this.$route.path
        ),
        1
      )
      this.$router.push({
        path: '/newProject/drillHoleList'
      })
    }
  }
}
</script>
